package com.tiancheng.onecode.service.impl;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.tiancheng.onecode.configuration.WxMaProperties;
import com.tiancheng.onecode.enums.ClientSourceEnums;
import com.tiancheng.onecode.exception.Asserts;
import com.tiancheng.onecode.model.request.MiniLoginRequest;
import com.tiancheng.onecode.service.ILoginService;
import com.tiancheng.onecode.service.WechatMiniAppService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @ClassName LoginServiceImpl
 * @Description TODO
 * @Author kellen
 * @Date 2024/12/7 17:01
 */
@Slf4j
@Service
@AllArgsConstructor
public class LoginServiceImpl implements ILoginService {

    private final WechatMiniAppService wechatMiniAppService;
    private final WxMaProperties wxMaProperties;
    @Override
    public void miniLogin(MiniLoginRequest miniLoginRequest) {
        // 9313c26eace7d77046f4433b551083be
        Asserts.isTrue(ClientSourceEnums.getByCode(miniLoginRequest.getClientSource()).isPresent(), "登录失败，不支持的登录类型！");
        final WxMaJscode2SessionResult login = wechatMiniAppService.login(wxMaProperties.getAppid(), miniLoginRequest.getCode());
        Asserts.isTrue(null != login && StringUtils.isNoneEmpty(login.getOpenid()), "微信登录失败！");
        String openId = login.getOpenid();
        String unionId = login.getUnionid();
        // 登录成功：openId:o8yaZ6zk3b01_9PhuXJd4VGHqwWA unionId:null
        log.info("登录成功：openId:{} unionId:{}",openId,unionId);
        

    }
}
